﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityTouchTable.Editor;

namespace UnityTouchTable.Example.Editor
{
    [CustomEditor(typeof(MarkerItem), true)]
    [CanEditMultipleObjects]
    public class MarkerItemEditor : MarkerItemBaseEditor
    {
        #region Field
        private SerializedProperty m_CenterNode;
        private SerializedProperty m_SubContentNodes;
        private SerializedProperty m_AngleEqualizer;

        private ReorderableList m_SubContents;
        #endregion

        #region Function
        protected override void OnEnable()
        {
            base.OnEnable();

            this.m_CenterNode = this.serializedObject.FindProperty("m_CenterNode");
            this.m_SubContentNodes = this.serializedObject.FindProperty("m_SubContentNodes");
            this.m_AngleEqualizer = this.serializedObject.FindProperty("m_AngleEqualizer");

            this.m_SubContents = new ReorderableList(this.serializedObject, this.m_SubContentNodes, true, true, true, true);
            this.m_SubContents.drawHeaderCallback = this.OnSubContentsDrawHead;
            this.m_SubContents.drawElementCallback = this.OnSubContentsDrawElement;
        }

        public override void OnInspectorGUI()
        {
            this.serializedObject.Update();

            this.DrawMarkItemBaseProperty();
            EditorGUILayout.PropertyField(this.m_CenterNode, new GUIContent("中心节点"));
            EditorGUILayout.PropertyField(this.m_AngleEqualizer, new GUIContent("角度均分器"));
            this.m_SubContents.DoLayoutList();

            this.serializedObject.ApplyModifiedProperties();
        }

        /// <summary>
        /// 子内容绘制标题时
        /// </summary>
        /// <param name="rect"></param>
        private void OnSubContentsDrawHead(Rect rect) => GUI.Label(rect, "子内容集");

        /// <summary>
        /// 绘制子内容时
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="index"></param>
        /// <param name="isActive"></param>
        /// <param name="isFocused"></param>
        private void OnSubContentsDrawElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty itemData = this.m_SubContents.serializedProperty.GetArrayElementAtIndex(index);
            EditorGUI.PropertyField(rect, itemData, GUIContent.none);
        }
        #endregion
    }
}